﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using SharedBusinessLogic.Security;
using Base.Users;

namespace SharedBusinessLogic.Security
{
	public sealed class TpPrincipal : IPrincipal
	{
		#region Members

		public const string EmployeeRoleName = "Employee";
		public const string ReaderRoleName = "Reader";
		private TpIdentity identity;

		#endregion

		#region Construction

		public TpPrincipal()
		{
		}

		#endregion

		#region Properties

		IIdentity IPrincipal.Identity
		{
			get
			{
				return identity;
			}
		}

		public bool IsAuthenticated
		{
			get
			{
				return true;
			}
		}

		#endregion

		#region Methods

		public bool IsInRole(string role)
		{
			if (role == null)
				throw new ArgumentNullException("role");

			if (identity.User == null || identity.User.Roles.Length == 0)
				return false;

			foreach (Role r in identity.User.Roles)
			{
				if (String.Compare(r.Name, role,
				    StringComparison.InvariantCultureIgnoreCase) == 0)

					return true;
			}

			return false;
		}

		#endregion


	}
}
